#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
  pid_t pid = fork();
  if(pid == 0) {
    int cnt = 5;
    while(cnt) {
      printf("child[%d] is running : cnt is : %d\n", getpid(), cnt);
      cnt--;
      sleep(1);
    }
    exit(1);
  }
  int status = 0;
  while(1){
    pid_t ret = waitpid(pid, &status, WNOHANG);
    sleep(1);
    if(ret == 0) {
      printf("do parent's thing\n");
    }else if(ret > 0) {
      if(WIFEXITED(status)){
        printf("exit code: %d\n", WEXITSTATUS(status));
        break;
      }
    }else {
      printf("perror\n");
      break;
    }
  }
}
